package com.bridgeintelligent.tag.customer.controller;

import com.bridgeintelligent.tag.customer.entity.AnalysisTemplate;
import com.bridgeintelligent.tag.customer.entity.Result;
import com.bridgeintelligent.tag.customer.service.IAnalysisTemplateService;
import com.bridgeintelligent.tag.utils.PasswordHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Api(tags = "画像模板管理")
@AllArgsConstructor
@RestController
@RequestMapping("/api/analysis")
@Slf4j
public class AnalysisTemplateController {

    private IAnalysisTemplateService templateService;

    @ApiOperation("新增模板")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "template", value = "模板信息")
    })
    @PostMapping("/add")
    public Result addAnalysisTemplate(@RequestBody AnalysisTemplate template) {
        try {
            //校验特殊字符
            if ( PasswordHelper.checkSpecial(template.getTlName())){
                return new Result(-1,"名字含有特殊字符！");
            }
            //校验模板名是否重复
            boolean success = templateService.checkTlName(template.getId(),template.getTlName());
            if (success){
                return new Result(-1, "模板名称重复，请修改模板名称后重新提交！");
            }
            templateService.addAnalysisTemplate(template);
            return new Result(0,"保存成功");
        } catch (Exception e) {
            log.error("新增模板失败：{}::",e);
            return new Result(-1,"新增失败");
        }
    }


    @ApiOperation("修改模板")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", name = "template", value = "模板信息")
    })
    @PostMapping("/update")
    public Result updateAnalysisTemplate(@RequestBody AnalysisTemplate template) {
        try {
            //校验特殊字符
            if ( PasswordHelper.checkSpecial(template.getTlName())){
                return new Result(-1,"名字含有特殊字符！");
            }
            //校验模板名是否重复
            boolean success = templateService.checkTlName(template.getId(),template.getTlName());
            if (success) {
                return new Result(-1, "模板名称重复，请修改模板名称后重新提交！");
            }
            templateService.updateAnalysisTemplate(template);
            return new Result(0,"修改成功");
        } catch (Exception e) {
            log.error("修改模板失败：{}::"+e);
            return new Result(-1, "修改失败");
        }
    }

    @ApiOperation("删除模板")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "模板ID")})
    @GetMapping("/del")
    public Result delAnalysisTemplate(String id) {
        try {
            templateService.delAnalysisTemplate(id);
            return new Result(0,"删除成功");
        }catch (Exception e){
            log.error("删除模板失败：{}::"+e);
            return new Result(-1,"删除失败");
        }

    }

    @ApiOperation("模板详情")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "id", value = "模板ID")
    })
    @GetMapping("/findOne")
    public Result findOne(String id) {
        try {
            AnalysisTemplate template = templateService.findAnalysisTemplate(id);
            return new Result(0,"查询成功",template);
        }catch (Exception e){
            log.error("查看模板详情失败：{}::"+e);
            return new Result(-1,"查看模板详情失败");
        }
    }

    @ApiOperation("分页模板列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNumber", value = "页码"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数"),
            @ApiImplicitParam(paramType = "query", name = "tlName", value = "模板名称"),
            @ApiImplicitParam(paramType = "query", name = "stTime", value = "开始时间"),
            @ApiImplicitParam(paramType = "query", name = "edTime", value = "结束时间"),
            @ApiImplicitParam(paramType = "query", name = "indexId", value = "索引ID")
    })
    @GetMapping("/pageList")
    public Result pageList(Integer pageNumber, Integer pageSize, String tlName,String stTime, String edTime,String indexId) {
        try {
            return templateService.pageList(pageNumber,pageSize,tlName,stTime,edTime,indexId);
        }catch (Exception e){
            log.error("分页查询失败：{}::"+e);
            return new Result(-1,"分页查询失败");
        }
    }
}